﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DTOFrameworks;
using PhysicalDataProvider;
using FooFun.DataElements.Dtos;
using FooFun.Web.Utils;

namespace FAQFrameworks
{
    public class FAQContext
    {
        public static FAQContext Current
        {
            get
            {
                HttpContext current = HttpContext.Current;
                FAQContext context = new FAQContext(current);

                return context;
            }
        }

        private FAQContext(HttpContext httpContext)
        {
            this.httpContext = httpContext;
        }

        HttpContext httpContext;

        // todo: Cookie名字成为配置
        public LoginUser User
        {
            get
            {
                // 测试用
                if (httpContext == null)
                {
                    return LoginUser.AnonymousUser;
                }
                
                HttpCookie cookie
                    = this.httpContext.Request.Cookies["ID"];

                if (cookie != null)
                {
                    Guid guid
                        = SiteUrlsProvider.GetGuidFromUrl(cookie.Value);
                    if (guid != Guid.Empty)
                    {
                        LoginUser user = new LoginUser();
                        user.AccountId = guid;

                        return user;
                    }
                }

                return LoginUser.AnonymousUser;
            }
        }

        public string ReturnUrl
        {
            get
            {
                // 其中已经进行了URL解码
                return this.httpContext.Request.Params["ReturnUrl"];
            }
        }

        public Guid ForumId
        {
            get
            {
                Guid guid = GetGuidFromParams("ForumId");
                if (guid == Guid.Empty)
                {
                    //  从TopicId从获得ForumId
                    if (TopicId != Guid.Empty)
                    {
                        TopicManager topicManager =
                            new TopicManager();
                        IDataElement topic
                            = topicManager.GetElement(TopicId);

                        if (!topic.IsEmpty)
                        {
                            guid = DataProviderUtil.GetGuid(
                                (topic["@ForumId"].Value));
                        }
                    }
                }

                return guid;
            }
        }

        public Guid TopicId
        {
            get
            {
                return GetGuidFromParams("TopicId");
            }
        }

        public Guid ReplyId
        {
            get
            {
                return GetGuidFromParams("ReplyId");
            }
        }

        public string ClientIP
        {
            get
            {
                if (httpContext == null)
                {
                    return "0.0.0.0";
                }

                return this.httpContext.Request.UserHostAddress;

            }
        }

        public Guid AccountId
        {
            get
            {
                Guid accountId = GetGuidFromParams("AccountId");
                if (accountId == Guid.Empty)
                {
                    accountId = this.User.AccountId;
                }

                return accountId;
            }
        }

        public int PageIndex
        {
            get
            {
                int pageIndex = GetIntFromParams("PageIndex", 0);
                return pageIndex;
            }
        }

        private int GetIntFromParams(string key, int defaultValue)
        {
            int i = 0;

            string str
                = this.httpContext.
                Request.Params[key];

            if (int.TryParse(str, out i))
            {
                return i;
            }
            return defaultValue;
        }

        private Guid GetGuidFromParams(string key)
        {
            Guid guid = Guid.Empty;

            string str = this.httpContext.Request.Params[key];
            try
            {
                guid = DataProviderUtil.GetGuid(str);
            }
            catch { }

            return guid;
        }
    }
}
